//
//  oldFamilyEditRelationViewController.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/3/30.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldFamilyEditRelationViewController.h"
#import "oldMyParentsViewController.h"

@interface oldFamilyEditRelationViewController ()
@property (weak, nonatomic) IBOutlet UITextField *text_field;

@end

@implementation oldFamilyEditRelationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    self.navigationItem.title = @"亲属关系";
    
    UIButton *rightbutton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
    
    //[leftbutton setBackgroundColor:[UIColor blackColor]];
    
    [rightbutton setTitle:@"保存" forState:UIControlStateNormal];
    
    [rightbutton addTarget:self action:@selector(rightbtnTapped:) forControlEvents:UIControlEventTouchDown];
    
    UIBarButtonItem *rightitem = [[UIBarButtonItem alloc]initWithCustomView:rightbutton];
    
    self.navigationItem.rightBarButtonItem = rightitem;
    
    self.view.backgroundColor = Color_With_Rgb(247, 247, 247, 1);
}

- (void)rightbtnTapped:(id)sender
{
    
    if (![_text_field.text length]) {
        [oldHub warningUser:@"请输入亲属关系"];
        return;
    }
    
    [oldHub waiting:@"保存中.."];
    
    NSCharacterSet  *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *relation = [_text_field.text stringByTrimmingCharactersInSet:set];
    
    WeakSelf
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: _parent.userRelation.rid, @"rid", relation, @"relation", nil];
    
    [OLDNetWork httpRequestWithPostPut:URL_EditRelation params:params success:^(id data) {
        [oldHub hideHUD];
        weakSelf.parent = [[oldServer alloc] initWithDictionary:data];
        [weakSelf editSuccess];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

#pragma mark - initData
- (void)initData
{
    if (_parent.userRelation.relation) {
        _text_field.text = _parent.userRelation.relation;
    }
}

- (void)editSuccess
{
    UIViewController *vc = self.navigationController.viewControllers[[self.navigationController.viewControllers count] - 2];
    
    if ([vc isKindOfClass:[oldMyParentsViewController class]]) {
        oldMyParentsViewController *oldVc = (oldMyParentsViewController *)vc;
        oldVc.parent = _parent;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
